﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using DragDraw.Util;
using DragDraw.UserService;

namespace DragDraw.Tool
{
    public partial class PhotoPlayer : UserControl, IPhotoPlayer
    {
        public List<Photo> Photos { get; set; }
        private int m_interval = 10;
        private DispatcherTimer m_timer;
        private int m_curIndex;
        private bool m_playing;
        private WidgetServiceProxy ServiceProxy;
        private WidgetSpaceProxy SpaceProxy;

        private PhotoBoard m_board;
        private PhotoLargeViewer m_viewer;

        public PhotoPlayer()
        {
            InitializeComponent();
            Photos = new List<Photo>();
            m_timer = new DispatcherTimer();
            m_timer.Interval = new TimeSpan(0, 0, m_interval);
            m_timer.Tick += OnTimer;
            m_timer.Start();
            m_playing = true;
            m_curIndex = 0;
            m_board = new PhotoBoard();
            m_viewer = new PhotoLargeViewer();
            Controller.Visibility = Visibility.Collapsed;
            Play.Visibility = Visibility.Collapsed;
            Pause.Visibility = Visibility.Visible;
            Photos.Add(new Photo() { Name = "", ImgUrl = "" });

            if (WidgetHelper.IsInMySpace == false)
                SettingButton.Visibility = Visibility.Collapsed;
        }
        public void SavePhotos()
        {
            StringWriter sw = new StringWriter();
            XmlSerializer xs = new XmlSerializer(typeof(List<Photo>));
            xs.Serialize(sw, Photos);
            ServiceProxy.SetSession("Photos", sw.ToString());
        }
        private void PictureRefresh()
        {
            try
            {
                string url = Photos[m_curIndex].ImgUrl;
                if (url.Contains(@"http://") == false)
                    url = WidgetHelper.MapFileUrl(
                        OperationManager.Instance.CurrentSpace.OwnerUserID, url);
                MainPicture.Source = new BitmapImage(new Uri(url));
            }
            catch 
            {
                MainPicture.Source = new BitmapImage();
            }
        }

        private void OnGetSession(object sender, DragDraw.UserService.GetSessionCompletedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.Result))
            {
            }
            else
            {
                try
                {
                    XmlSerializer xs = new XmlSerializer(typeof(List<Photo>));
                    XmlReader xr = XmlReader.Create(new StringReader(e.Result));

                    Photos = (List<Photo>)xs.Deserialize(xr);
                    m_curIndex = 0;
                }
                catch
                {
                    Photos.Clear();
                }
            }
            PictureRefresh();
        }

        private void OnSetSession(object sender, DragDraw.UserService.SetSessionCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                ErrorManager.Instance.ShowLastServiceError();
            }
        }

        private void OnTimer(object sender, EventArgs e)
        {
            if (m_playing)
            {
                m_curIndex = (m_curIndex + Photos.Count + 1) % Photos.Count;
                PictureRefresh();
            }
        }

        private void Previous_Click(object sender, RoutedEventArgs e)
        {
            m_curIndex = (m_curIndex + Photos.Count - 1) % Photos.Count;
            PictureRefresh();
        }

        private void Play_Click(object sender, RoutedEventArgs e)
        {
            m_playing = true;
            m_timer.Start();
            Play.Visibility = Visibility.Collapsed;
            Pause.Visibility = Visibility.Visible;
        }

        private void Pause_Click(object sender, RoutedEventArgs e)
        {
            m_playing = false;
            m_timer.Stop();
            Play.Visibility = Visibility.Visible;
            Pause.Visibility = Visibility.Collapsed;
        }

        private void Next_Click(object sender, RoutedEventArgs e)
        {
            m_curIndex = (m_curIndex + Photos.Count + 1) % Photos.Count;
            PictureRefresh();
        }

        private void SettingButton_Click(object sender, RoutedEventArgs e)
        {
            m_board.Init(this);
            SpaceProxy.ShowModalDialog(m_board, "相册设置");
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ServiceProxy = OperationManager.Instance.CreateWidgetServiceProxy(this);
            ServiceProxy.GetSessionCompleted += OnGetSession;
            ServiceProxy.SetSessionCompleted += OnSetSession;
            SpaceProxy = OperationManager.Instance.CreateWidgetSpaceProxy(this);

            ServiceProxy.GetSession("Photos");
        }

        private void UserControl_MouseEnter(object sender, MouseEventArgs e)
        {
            Controller.Visibility = Visibility.Visible;
        }

        private void UserControl_MouseLeave(object sender, MouseEventArgs e)
        {
            Controller.Visibility = Visibility.Collapsed;
        }

        private void MainPicture_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            m_viewer.SetPhotoList(Photos);
            m_viewer.Index = m_curIndex;
            m_viewer.Init();
            SpaceProxy.ShowModalDialog(m_viewer,"相册");
        }
    }

    public class Photo
    {
        public string ImgUrl { get; set; }
        public string Name { get; set; }
        public Photo()
        {
            ImgUrl = "";
            Name = "";
        }
    }

    public class Album
    {
        public string AlbumName;
        public List<Photo> Photos;
    }
}
